package com.bawei.module_mine.mine

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.bawei.lib_common.base.BaseApplication
import com.bawei.lib_common.base.BaseViewModel
import com.bawei.lib_common.base.state.Failed
import com.bawei.lib_common.base.state.Loading
import com.bawei.lib_common.base.state.RequestEvent
import com.bawei.lib_common.base.state.Success
import com.bawei.lib_common.base.utils.ToastUtils
import com.bawei.lib_net.storage.AuthMMKV
import com.bawei.module_mine.R
import com.bawei.module_mine.model.MineServiceModel
import com.bawei.module_mine.model.ServiceStatisticsModel
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class MineViewModel @Inject constructor(
    private val mRepository: MineRepository
) :BaseViewModel() {
    //登录账号密码
    val phone: MutableStateFlow<String> = MutableStateFlow("")
    val password: MutableStateFlow<String> = MutableStateFlow("")
    //注册账号密码
    val phoneReg: MutableStateFlow<String> = MutableStateFlow("")
    val passwordReg: MutableStateFlow<String> = MutableStateFlow("")
    //忘记密码的新密码
    val newPassword: MutableStateFlow<String> = MutableStateFlow("")
    //我的页面的数据集合
    private val _serviceList = MutableLiveData<List<MineServiceModel>>()
    val serviceList: LiveData<List<MineServiceModel>> get() = _serviceList

    private val _statisticsList = MutableLiveData<List<ServiceStatisticsModel>>()
    val statisticsList: LiveData<List<ServiceStatisticsModel>> get() = _statisticsList
    //登录请求事件
    private val _requestLoginEvent = MutableSharedFlow<RequestEvent>()
    val requestLoginEvent get() = _requestLoginEvent.asSharedFlow()
    //注册请求事件
    private val _requestRegisterEvent = MutableSharedFlow<RequestEvent>()
    val requestRegisterEvent get() = _requestRegisterEvent.asSharedFlow()
    //修改密码请求事件
    private val _requestModifyPassEvent = MutableSharedFlow<RequestEvent>()
    val requestModifyPassEvent get() = _requestModifyPassEvent.asSharedFlow()
    //账号密码登录
    fun passLogin() {
        execute(
            action = mRepository.passLogin(password.value, phone.value),
            onStart = {
                viewModelScope.launch {
                    _requestLoginEvent.emit(Loading)
                }
            },
            onError = {
                viewModelScope.launch {
                    _requestLoginEvent.emit(Failed(it))
                }
            },
            onFinish = {
                AuthMMKV.token = it.data.token
                AuthMMKV.uId = it.data.id.toString()
                AuthMMKV.username = it.data.username
                viewModelScope.launch {
                    _requestLoginEvent.emit(Success)
                }
            }
        )
    }
    //账号密码注册
    fun passRegister() {
        execute(
            action = mRepository.passRegister(passwordReg.value, phoneReg.value),
            onStart = {
                viewModelScope.launch {
                    _requestRegisterEvent.emit(Loading)
                }
            },
            onError = {
                viewModelScope.launch {
                    _requestRegisterEvent.emit(Failed(it))
                }
            },
            onFinish = {
                viewModelScope.launch {
                    _requestRegisterEvent.emit(Success)
                }
            }
        )
    }
    //修改密码
    fun modifyPassword() {
        execute(
            action = if (!AuthMMKV.uId.isBlank()){
                mRepository.modifyPassword(newPassword.value,AuthMMKV.uId)
            }else{
                ToastUtils.showLong(BaseApplication.instance,"请先登录然后修改密码")
               return
            },
            onStart = {
                viewModelScope.launch {
                    _requestModifyPassEvent.emit(Loading)
                }
            },
            onError = {
                viewModelScope.launch {
                    _requestModifyPassEvent.emit(Failed(it))
                }
            },
            onFinish = {
                viewModelScope.launch {
                    _requestModifyPassEvent.emit(Success)
                }
            }
        )
    }
    // 模拟网络请求获取服务列表
    fun loadServiceList() {
        viewModelScope.launch {
            // 模拟网络延迟（可选）
            delay(500)
            // 本地数据
            val localData = listOf(
                MineServiceModel(0, R.drawable.icon_mine_one, "我的申请"),
                MineServiceModel(1, R.drawable.icon_mine_two, "我的审核"),
                MineServiceModel(2, R.drawable.icon_mine_there, "我的公告"),
                MineServiceModel(3, R.drawable.icon_mine_four, "人员管理"),
                MineServiceModel(4, R.drawable.icon_mine_five, "访客管理"),
                MineServiceModel(5, R.drawable.icon_mine_six, "物业服务")
            )
            _serviceList.value = localData // 更新 LiveData
        }
    }
    fun loadServiceNoLoginList() {
        viewModelScope.launch {
            // 模拟网络延迟（可选）
            delay(500)
            // 本地数据
            val localData = listOf(
                MineServiceModel(0, R.drawable.icon_mine_no_one, "访客管理"),
                MineServiceModel(1, R.drawable.icon_mine_no_two, "物业服务"),
                MineServiceModel(2, R.drawable.icon_mine_no_there, "社区活动"),
                MineServiceModel(3, R.drawable.icon_mine_no_four, "设施预约"),
                MineServiceModel(4, R.drawable.icon_mine_no_five, "生活服务"),
                MineServiceModel(5, R.drawable.icon_mine_no_six, "门禁管理")
            )
            _serviceList.value = localData // 更新 LiveData
        }
    }

    // 模拟网络请求获取统计数据
    fun loadStatisticsList() {
        viewModelScope.launch {
            delay(500)
            val localData = listOf(
                ServiceStatisticsModel(0, "本月报修", "156"),
                ServiceStatisticsModel(1, "处理率", "98%"),
                ServiceStatisticsModel(2, "满意度", "4.8")
            )
            _statisticsList.value = localData // 更新 LiveData
        }
    }
}